﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace NowFloats.RichSnippetExtractor.MicroFormat
{
    class MicroFormatRequest
    {
        public float? GetAggregateRatings(String Url)
        {
            HtmlDocument document = (new HtmlWeb().Load(Url));
            return GetAggregateRatings(document);
        }

        public float? GetAggregateRatings(HtmlDocument document)
        {
            foreach (var node in document.DocumentNode.Descendants())
            {
                if (node.Attributes.Contains("class"))
                {
                    if ((node.Attributes["class"].Value.ToLower()).Contains("hreview-aggregate"))
                    {
                        foreach(var childNode in node.Descendants())
                        {
                            if (childNode.Attributes.Contains("class"))
                            {
                                if((childNode.Attributes["class"].Value.ToLower()).Contains("average"))
                                {
                                    float rating;
                                    if (childNode.HasChildNodes)
                                    {
                                        foreach (var ratingNode in childNode.Descendants())
                                        {
                                            if(ratingNode.Attributes.Contains("class"))
                                            {
                                                if (ratingNode.Attributes["class"].Value.ToLower().Contains("value-title"))
                                                {
                                                    if (!String.IsNullOrEmpty(ratingNode.InnerText))
                                                    {
                                                        if (float.TryParse(ratingNode.InnerText, out rating))
                                                            return rating;
                                                    }
                                                    else
                                                    {
                                                        if (ratingNode.Attributes.Contains("title"))
                                                        {
                                                            if (float.TryParse(ratingNode.Attributes["title"].Value, out rating))
                                                                return rating;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (float.TryParse(childNode.InnerText, out rating))
                                            return rating;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return null;
        }
    }
}
